package com.chis.auth;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;


public class Test {



    public static String post (String url, String content)
            throws NoSuchAlgorithmException, KeyManagementException,
            IOException {
        try {
            URL console = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) console.openConnection();
            conn.setDoOutput(true);
            conn.addRequestProperty("connection", "Keep-Alive");
            conn.connect();
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.write(content.getBytes("utf-8"));
            // 刷新、关闭
            out.flush();
            out.close();
            InputStream is = conn.getInputStream();
            if (is != null) {
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }
                is.close();
                if(outStream!=null){
                    return new String(outStream.toByteArray(),"utf-8");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        try {
            String opt = args[0];
            String content = args[1];
            if(opt.equals("1")){
                String str=Test.post("http://10.41.3.163:18090/generateQRPNG", content);
                System.out.println(str);
            } else if (opt.equals("2")){
                String str=Test.post("http://10.41.3.163:18090/checkCode", content);
                System.out.println(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    

}
